package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.User;
import com.example.shengyin.service.AdminService;
import com.example.shengyin.service.UserService;
import com.example.shengyin.util.Md5Utils;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * AdminController
 * 
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/user")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api("用户")
//redis缓存用
public class UserController
{
    @Autowired
    private UserService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/login")
    public Result userLogin(String root, String pass, String code, HttpServletRequest request) {

        System.out.println(code+"================?"+request.getSession().getAttribute("code"));
        if(!code.equals(request.getSession().getAttribute("code"))){
            return Result.error("验证码输入错误");
        }
        User login = service.login(root, pass);
        if(login==null)
            return Result.error("错误的用户名或者密码！");
        HttpSession session = request.getSession();
        session.setAttribute("user",login);
        String key= UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(key,login,30, TimeUnit.MINUTES);
        return Result.success(key,"登录成功");
    }
    /**
     * 查询Admin列表
     */
    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Result list(Page page,
                       @ApiParam(name = "phone",value = "手机号") @RequestParam(required = false) String phone)
    {
        System.out.println("Userpage:"+page);
        List<User> list = service.list(page,phone);
        int count = service.count(phone);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }
    @ApiOperation("单个查找")
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        User select = service.select(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @ApiOperation("添加管理员")
    @PostMapping("/add")
    public Result insert(@Valid User dAdmin, BindingResult bindingResult)
    {
        dAdmin.setPass(Md5Utils.hash(dAdmin.getPass()));
        int insert = service.insert(dAdmin);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Result edit(User dAdmin)
    {
        int insert = service.update(dAdmin);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }
    @ApiOperation("删除单个")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id)
    {
        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @ApiOperation("删除多个")
	@GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }


    @ApiOperation("是否显示")
    @GetMapping("/isvalue/{id}/{isvalue}")
    public Result isvalue(@PathVariable(value = "id")Long id,@PathVariable(value = "isvalue")Boolean isvalue)
    {
        User dAdmin=new User();
        dAdmin.setId(id);
        if(isvalue==true)
            dAdmin.setIsvalid((byte) 1);
        int insert = service.update(dAdmin);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }
}
